home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10349 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Pointers to Arrays (or not)
  5. Date: 7 Mar 1996 19:31:47 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4hndj3$mgj@dawn.mmm.com>
  8. References: <4hkbg9$cja@aphex.direct.ca>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. qjackson@direct.ca wrote:
  13. > Hello:
  14.  
  15. > I've come across something that I'm wondering about.
  16.  
  17. > I have a class, Foo, with a member that is a pointer to Bar, where Bar
  18. > can be either a single object of Bar, or an array of Bar, depending on
  19. > other semantics:
  20.  
  21.  
  22. > class Foo
  23. > {
  24.  
  25. >     Bar* pBar;
  26. >     int barTab; // how many Bar's are pointed to be pBar
  27.  
  28. >     ~Foo (void);
  29.  
  30. > };
  31.  
  32. > I would have thought that I could kill pBar this way:
  33.  
  34. >     delete [] pBar;
  35.  
  36. > but that crashes if pBar was instantiated thus:
  37.  
  38. >     pBar = new Bar;
  39.  
  40. [destructor with switch case elided]
  41.  
  42. > What am I missing?
  43.  
  44. You are not missing anything.  There is a new/delete for arrays, and a
  45. new/delete for single objects.  The delete you use must match the new
  46. you use.
  47. --
  48. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  49. 3M Company                      phone:  (612) 737-4643
  50. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  51. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  52.     But 3M speaks for me -- I did not write the following line:
  53.  
  54. Opinions expressed herein are my own and may not represent those of 3M.
  55.